package com.qf.test;

import com.qf.utils.ESUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;

import java.io.IOException;

/**
 * term&terms查询
 */
public class TermDemo {
    /*
    //1. 创建指定的Request对象，并设置索引名称和doc_id

    //2. 设置添加的数据给Request对象

    //3. 通过连接对象将Request发送给ES，并接受Response

    //4. 通过Response判断添加是否成功
     */
    @Test
    public void term() throws IOException {
        //1. 创建指定的Request对象，并设置索引名称和doc_id
        SearchRequest request = new SearchRequest("sms-logs-index");

        //2. 设置添加的数据给Request对象
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.termQuery("corpName","途虎养车"));
        builder.from(0);
        builder.size(1);
        request.source(builder);

        //3. 通过连接对象将Request发送给ES，并接受Response
        SearchResponse resp = ESUtil.getClient().search(request, RequestOptions.DEFAULT);
        //4. 通过Response判断添加是否成功
        for (SearchHit hit : resp.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());
        }
    }

}
